// firstPackage/pages/reportModule/setCompanyHouseStation/setCompanyHouseStation.js
import {
  queryCompanyEmplyeeStation,

} from "../../../../service/team_service"

import { setCompanyHouseStationRequest } from "../../../../service/platform_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    page: 0,
    pageSize: 200,
    companyList: [],
    houseNumber: "",
    companyNumber: "",
    selectArr: [],//已经选中的驻场人员
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber: options.houseNumber
    })
    // console.log("JJLog:houseNumber=:" + this.data.houseNumber);
    this.queryCompanyEmplyeeStation();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //公司列表:
  queryCompanyEmplyeeStation() {
    wx.showLoading();
    queryCompanyEmplyeeStation(this.data.houseNumber, this.data.page, this.data.pageSize).then(res => {
      wx.hideLoading();
      console.log("JJLog:公司列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          companyList: res.data || []
        })
        if (res.data && res.data.length > 0) {
          this.dealManager(res.data);
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryCompanyEmplyeeStation", err);
    })
  },
  //处理已选中的驻场
  dealManager(arr) {
    let tempArr = [];
    arr.forEach((i) => {
      if (i.isResident) {
        tempArr.push(i.id);
      }
    })
    this.setData({
      selectArr: tempArr
    })
  },

  //选择员工逻辑
  selectEmAction(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let isChoose = item.isResident;
    if (isChoose) {
      //已经选中的情况
      let item = 'companyList[' + index + '].isResident';
      this.setData({
        [item]: false,
      })
    } else {
      let item = 'companyList[' + index + '].isResident';
      this.setData({
        [item]: true,
      })
    }

    let tempArr = [];
    this.data.companyList.forEach((i) => {
      if (i.isResident) {
        tempArr.push(i.id);
      }
    })
    setTimeout(() => {
      this.setData({
        selectArr: tempArr
      })
    }, 500);

  },
  //确定选择:
  sureSelectAction() {
    if (this.data.selectArr && this.data.selectArr.length < 1) {
      wx.showToast({
        title: '请至少选中一个员工',
        icon: "none"
      })
      return;
    }
    debounce(this.setCompanyHouseStationRequest);
  },
  setCompanyHouseStationRequest() {
    wx.showLoading();
    let data = {
      employeeIds: this.data.selectArr,
      houseNumber: this.data.houseNumber
    }
    setCompanyHouseStationRequest(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '设置驻场成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:setCompanyHouseStationRequest", err);
    })
  },

})